package com.jindreamit.m3u8.download;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jindreamit.m3u8.download.jwt.LoginForJWT;
import com.jindreamit.m3u8.download.runnable.DownloadRunnable;
import com.jindreamit.m3u8.download.runnable.ReadM3u8Runnable;
import com.jindreamit.m3u8.download.util.RedisUtils;
import com.jindreamit.m3u8.download.util.ThreadPoolUtils;
import org.jsoup.Jsoup;

import java.util.concurrent.ExecutorService;

public class Application {

    public static final String prefix = "/home/xinyue/m3u8/";

    //同步学堂下载
    static private void tbxtDownload() throws Exception {
        int n = 0;
        JSONArray array = JSON.parseArray(RedisUtils.get("tbxt"));
        for (int i = 0; i < array.size(); i++) {
            final String baseURL = "http://sxtx.szqnkf.com/api/gl/getCatalogList.php";
            JSONObject object = array.getJSONObject(i);
//            System.out.println(object.toString());
            String xueduan = object.getString("xueduan");
            String nianji = object.getString("nianji");
            String grade = object.getString("title");
            JSONArray all_course = object.getJSONArray("all_course");
            for (int j = 0; j < all_course.size(); j++) {
                JSONObject course = all_course.getJSONObject(j);
                String xueke = course.getString("xueke");
                String banben = course.getString("banben");
                //年级 例如：小学一年级
//                System.out.println(grade);
                String coursesJSON = Jsoup.connect(baseURL)
                        .data("xueduan", xueduan)
                        .data("nianji", nianji)
                        .data("xueke", xueke)
                        .data("banben", banben)
                        .ignoreContentType(true)
                        .get()
                        .body()
                        .text();
                try {
                    System.out.println("0-1 === "+JSONObject.parseObject(coursesJSON));
                }catch (Exception e){
                    e.printStackTrace();
                    continue;
                }
                if (JSONObject.parseObject(coursesJSON).getString("code").equals("8"))continue;
                JSONArray resultArray = JSONObject.parseObject(coursesJSON)
                        .getJSONObject("result")
                        .getJSONArray("Result");
                if (null == resultArray) continue;
                for (int k = 0; k < resultArray.size(); k++) {
                    long mid = resultArray.getJSONObject(k)
                            .getLong("id");
                    //教材名称
                    String teachingMaterial = resultArray.getJSONObject(k)
                            .getString("name");

                    String videoListJSON = Jsoup.connect("http://sxtx.szqnkf.com/api/gl/getVideoList.php")
                            .data("mid", "" + mid)
                            .data("num", "200")
                            .ignoreContentType(true)
                            .get()
                            .body()
                            .text();
                    System.out.println("1 === " + videoListJSON);
                    JSONArray videoListArray=null;
                    try {
                        videoListArray = JSONObject.parseObject(videoListJSON)
                                .getJSONArray("result");
                    }catch (Exception e){
                        e.printStackTrace();
                        continue;
                    }
                    if (videoListArray != null) {
                        n += videoListArray.size();
                        RedisUtils.set("videoNum_"+grade+"/"+teachingMaterial,videoListArray.size()+"");
                    }
                    System.out.println("1 === " +n+"  "+ JSONObject.parseObject(videoListJSON).toString());
//                    for (int l = 0; l < videoListArray.size(); l++) {
//                        long id = videoListArray.getJSONObject(l)
//                                .getLongValue("id");
//                        String m3u8JSON = Jsoup.connect("http://sxtx.szqnkf.com/api/gl/getVideoUrl.php")
//                                .ignoreContentType(true)
//                                .header("jwt", LoginForJWT.getJWT())
//                                .data("mid", mid + "")
//                                .data("id", id + "")
//                                .get()
//                                .body()
//                                .text();
//                        String videoName = JSONObject.parseObject(m3u8JSON)
//                                .getJSONObject("result")
//                                .getString("name");
//                        String chaoqingURL = JSONObject.parseObject(m3u8JSON)
//                                .getJSONObject("result")
//                                .getString("chaoqing");
//                        ReadM3u8Runnable readM3u8Runnable = new ReadM3u8Runnable(chaoqingURL,(prefix+grade + "/" + teachingMaterial + "/" + videoName).replaceAll(" ",""));
//                        ThreadPoolUtils.execute(readM3u8Runnable);
//                    }
                }

            }
        }
        System.out.println(n);
        RedisUtils.set("n1", n + "");

    }

    static private int m3u8Number = 0;

    static private void dmktDownload() throws Exception {
        int n = 0;
        JSONArray array = JSON.parseArray(RedisUtils.get("dmkt"));
        for (int i = 0; i < array.size(); i++) {
            String id = array.getJSONObject(i)
                    .getString("id");
            String subject = array.getJSONObject(i)
                    .getString("subject");
            String courseListJSON = Jsoup.connect("http://sxtx.szqnkf.com/api/gl/getCourse.php")
                    .ignoreContentType(true)
                    .data("id", id)
                    .get()
                    .body()
                    .text();
            JSONArray courseList = JSONObject.parseObject(courseListJSON)
                    .getJSONArray("result");
            for (int j = 0; j < courseList.size(); j++) {
                JSONObject videoDetail = courseList.getJSONObject(j);
                String mid = videoDetail.getString("id");
                String courseName = videoDetail.getString("name");
                String videoListJSON = Jsoup.connect("http://sxtx.szqnkf.com/api/gl/getVideoList.php")
                        .ignoreContentType(true)
                        .data("mid", mid)
                        .data("num", "200")
                        .get()
                        .body()
                        .text();
                JSONArray videoList = JSONObject.parseObject(videoListJSON)
                        .getJSONArray("result");
                System.out.println("2 === " +n+" "+ JSONObject.parseObject(videoListJSON).toString());
                if (videoList != null) {
                    n += videoList.size();
                    RedisUtils.set("videoNum_"+subject+"/"+courseName,videoList.size()+"");
                }
//                for (int k = 0; k < videoList.size(); k++) {
//                    JSONObject video = videoList.getJSONObject(k);
//                    String videoName = video.getString("name");
//                    String videoId = video.getString("id");
//                    JSONObject m3u8JSON = JSONObject.parseObject(Jsoup.connect("http://sxtx.szqnkf.com/api/gl/getVideoUrl.php")
//                            .data("mid", mid)
//                            .data("id", videoId)
//                            .header("jwt", LoginForJWT.getJWT())
//                            .ignoreContentType(true)
//                            .get()
//                            .body()
//                            .text());
//                    String url = m3u8JSON.getJSONObject("result")
//                            .getString("gaoqing");
//                    ReadM3u8Runnable readM3u8Runnable = new ReadM3u8Runnable(url, (prefix+subject + "/" + courseName + "/" + videoName).replaceAll(" ",""));
//                    ThreadPoolUtils.execute(readM3u8Runnable);
//                }
            }
        }
        RedisUtils.set("n2", n + "");
    }

    static public void yezjgDownload() throws Exception {
        JSONArray array = JSON.parseArray(RedisUtils.get("yezjg"));
//        System.out.println(array);
        int n = 0;
        String courseType = "儿歌";
        for (int i = 0; i < array.size(); i++) {
            if (i > 5) courseType = "故事";
            if (i > 14) courseType = "国学";
            if (i > 21) courseType = "亲子";
            String id = array.getJSONObject(i)
                    .getString("id");
            String subject = array.getJSONObject(i)
                    .getString("subject");

            String videoListJSON = Jsoup.connect("http://sxtx.szqnkf.com/api/gl/getVideoList.php")
                    .ignoreContentType(true)
                    .data("mid", id)
                    .data("num", "200")
                    .get()
                    .body()
                    .text();
            JSONArray videoList = JSONObject.parseObject(videoListJSON)
                    .getJSONArray("result");
            System.out.println("3 === " +n+" "+ JSONObject.parseObject(videoListJSON).toString());
            if (videoList != null) {
                n += videoList.size();
                RedisUtils.set("videoNum_"+courseType+"/"+subject,videoList.size()+"");
            }
//            for (int k = 0; k < videoList.size(); k++) {
//                JSONObject video = videoList.getJSONObject(k);
//                String videoName = video.getString("name");
//                String videoId = video.getString("id");
//                JSONObject m3u8JSON = JSONObject.parseObject(Jsoup.connect("http://sxtx.szqnkf.com/api/gl/getVideoUrl.php")
//                        .data("mid", id)
//                        .data("id", videoId)
//                        .header("jwt", LoginForJWT.getJWT())
//                        .ignoreContentType(true)
//                        .get()
//                        .body()
//                        .text());
//                String url = m3u8JSON.getJSONObject("result")
//                        .getString("gaoqing");
//                ReadM3u8Runnable readM3u8Runnable = new ReadM3u8Runnable(url, (prefix+courseType + "/" + subject + "/" + videoName).replaceAll(" ",""));
//                ThreadPoolUtils.execute(readM3u8Runnable);
//            }

        }
        RedisUtils.set("n3", n + "");
    }

    public static void main(String[] args) throws Exception {
        new Thread(
                () -> {
                    try {
                        tbxtDownload();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
        ).start();

        new Thread(
                () -> {
                    try {
                        dmktDownload();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
        ).start();

        new Thread(
                () -> {
                    try {
                        yezjgDownload();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
        ).start();

    }
}
